﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi.DataVerificationRule
{
    /// <summary>
    /// 获取数据审核规则列表
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("DVR", "GetList", "1.0")]
    public class GetListAction : BaseAction<GetListParams, GetListResult>
    {
        protected override ActionResult<GetListResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetListParams opParams)
        {
            SER.DataVerificationRuleService service = new SER.DataVerificationRuleService();
            ListResult<ENT.EDataVerificationRule> serResult = service.GetList(opParams.DataTableId);
            if (serResult.IsSuccess)
            {
                GetListResult result = new GetListResult();
                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    GetListResult.Item item = new GetListResult.Item();
                    item.Id = serResult.Data[index].Id;

                    if (!string.IsNullOrEmpty(serResult.Data[index].Caption))
                    {
                        item.Caption = serResult.Data[index].Caption;
                    }
                    else
                    {
                        item.Caption = string.Empty;
                    }

                    item.Condition = serResult.Data[index].Condition;
                    item.Type = serResult.Data[index].Type;
                    result.Items.Add(item);
                }
                return new ActionResult<GetListResult>(result);
            }
            else
            {
                return new ActionResult<GetListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetListParams
    {
        [Parameter]
        public Guid DataTableId { get; set; }
    }

    public class GetListResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }

            public string Condition { get; set; }
            public string Caption { get; set; }

            /// <summary>
            /// 
            /// 规则类型，1脚本，2插件,3存储过程
            /// </summary>
            public int Type { get; set; }
        }
    }
}
